
#include "gtest/gtest.h"
#include "aidl_2_json_converter.h"
#include "../3rd/cjson/cJSON.h"

TEST(AIDL_2_JSON, null_string) {
  const char* aidl = "";

  char* out = aidl_2_json_converter_convert(aidl);
  ASSERT_EQ(TRUE, out != NULL);
  cJSON* root = cJSON_Parse(out);
  ASSERT_EQ(TRUE, root != NULL);
  ASSERT_EQ(TRUE, cJSON_IsObject(root));
  ASSERT_EQ(TRUE, root->child == NULL);
}

TEST(AIDL_2_JSON, package) {
  const char* aidl = "package org.kde.necessitas.ministro;";
  char* out = aidl_2_json_converter_convert(aidl);
  ASSERT_EQ(TRUE, out != NULL);

  cJSON* root = cJSON_Parse(out);
  ASSERT_EQ(TRUE, root != NULL);
  ASSERT_EQ(TRUE, root->child != NULL);
  cJSON* package = cJSON_GetObjectItem(root, "package");
  ASSERT_EQ(TRUE, package != NULL);
  ASSERT_EQ(TRUE, cJSON_IsArray(package));
  ASSERT_EQ(1, cJSON_GetArraySize(package));
  ASSERT_EQ(TRUE, strcmp(package->string, "package") == 0);
  cJSON* package_item_0 = cJSON_GetArrayItem(package, 0);
  ASSERT_EQ(TRUE, strcmp(package_item_0->valuestring, "org.kde.necessitas.ministro") == 0);
}

TEST(AIDL_2_JSON, import) {
  const char* aidl = "import org.kde.necessitas.ministro.IMinistroCallback;";
  char* out = aidl_2_json_converter_convert(aidl);
  ASSERT_EQ(TRUE, out != NULL);

  cJSON* root = cJSON_Parse(out);
  ASSERT_EQ(TRUE, root != NULL);
  ASSERT_EQ(TRUE, root->child != NULL);

  cJSON* import = cJSON_GetObjectItem(root, "import");
  ASSERT_EQ(TRUE, import != NULL);
  ASSERT_EQ(TRUE, cJSON_IsArray(import));
  ASSERT_EQ(1, cJSON_GetArraySize(import));
  ASSERT_EQ(TRUE, strcmp(import->string, "import") == 0);
  cJSON* import_item_0 = cJSON_GetArrayItem(import, 0);
  ASSERT_EQ(TRUE, strcmp(import_item_0->valuestring, "org.kde.necessitas.ministro.IMinistroCallback") == 0);


}

TEST(AIDL_2_JSON, interface) {
  const char* aidl = "interface IMinistro\
{\
    void requestLoader(in IMinistroCallback callback, in Bundle parameters);\
    void loaderReady(in Bundle loaderParams);\
}";

  char* out = aidl_2_json_converter_convert(aidl);
  ASSERT_EQ(TRUE, out != NULL);
  //printf("%s\n", out);

  cJSON* root = cJSON_Parse(out);
  ASSERT_EQ(TRUE, root != NULL);
  ASSERT_EQ(TRUE, root->child != NULL);

  cJSON* interface = cJSON_GetObjectItem(root, "interface");
  ASSERT_EQ(TRUE, interface != NULL);
  ASSERT_EQ(TRUE, cJSON_IsObject(interface));

  cJSON* interface_name = cJSON_GetObjectItem(interface, "interface_name");
  ASSERT_EQ(TRUE, strcmp(interface_name->string, "interface_name") == 0);
  ASSERT_EQ(TRUE, strcmp(interface_name->valuestring, "IMinistro") == 0);

  cJSON* function_array = cJSON_GetObjectItem(interface, "function_array");
  ASSERT_EQ(TRUE, function_array != NULL);
  ASSERT_EQ(TRUE, cJSON_IsArray(function_array));
  ASSERT_EQ(2, cJSON_GetArraySize(function_array));

  cJSON* function_0 = cJSON_GetArrayItem(function_array, 0);
  ASSERT_EQ(TRUE, function_0 != NULL);
  ASSERT_EQ(TRUE, cJSON_IsObject(function_0));

  cJSON* return_type = cJSON_GetObjectItem(function_0, "return_type");
  ASSERT_EQ(TRUE, return_type != NULL);
  ASSERT_EQ(TRUE, strcmp(return_type->string, "return_type") == 0);
  ASSERT_EQ(TRUE, strcmp(return_type->valuestring, "void") == 0);

  cJSON* function_name = cJSON_GetObjectItem(function_0, "function_name");
  ASSERT_EQ(TRUE, function_name != NULL);
  ASSERT_EQ(TRUE, strcmp(function_name->string, "function_name") == 0);
  ASSERT_EQ(TRUE, strcmp(function_name->valuestring, "requestLoader") == 0);

  cJSON* param_list = cJSON_GetObjectItem(function_0, "param_list");
  ASSERT_EQ(TRUE, param_list != NULL);
  ASSERT_EQ(TRUE, cJSON_IsArray(param_list));
  ASSERT_EQ(2, cJSON_GetArraySize(param_list));

  cJSON* param_0 = cJSON_GetArrayItem(param_list, 0);
  ASSERT_EQ(TRUE, param_0 != NULL);
  ASSERT_EQ(TRUE, cJSON_IsObject(param_0));
  
  cJSON* tag = cJSON_GetObjectItem(param_0, "param_tag");
  ASSERT_EQ(TRUE, tag != NULL);
  ASSERT_EQ(TRUE, strcmp(tag->string, "param_tag") == 0);
  ASSERT_EQ(TRUE, strcmp(tag->valuestring, "in") == 0);

  cJSON* param_type = cJSON_GetObjectItem(param_0, "param_type");
  ASSERT_EQ(TRUE, param_type != NULL);
  ASSERT_EQ(TRUE, strcmp(param_type->string, "param_type") == 0);
  ASSERT_EQ(TRUE, strcmp(param_type->valuestring, "IMinistroCallback") == 0);

  cJSON* param_name = cJSON_GetObjectItem(param_0, "param_name");
  ASSERT_EQ(TRUE, param_name != NULL);
  ASSERT_EQ(TRUE, strcmp(param_name->string, "param_name") == 0);
  ASSERT_EQ(TRUE, strcmp(param_name->valuestring, "callback") == 0);
}
